/*
 * GameLog.cpp
 *
 *  Created on: 2013. 10. 29.
 *      Author: jjaehuny
 */

#include "../GameLog.h"

#include "Static.h"
#include "../User.h"
#include "../Enum.h"
#include "../NResource/Static.h"
#include "../NLogic/Item.h"
#include <Database/Static.h>

namespace ProjectZ { namespace NGameLog {

void WriteMongoDB(User * pUser)
{
}

void WriteLogDB(User * pUser)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();
	pCommand->SetQueryType(::Database::Command::INSERT);
	pCommand->MakeQuery("INSERT INTO TEST VALUES (%d)", pUser->GetUserSeq());
	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(1000, pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}

}

void Login(User * pUser)
{

}

void DungeonReward(User * pUser) // 일반 카드, 젠 카드
{
	/*
	 * DROP TABLE IF EXISTS `zn_dungeon_reward_log`;

	CREATE TABLE `zn_dungeon_reward_log` (
	  `seq` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	  `dates` datetime NOT NULL,
	  `week` tinyint(4) NOT NULL,
	  `month` tinyint(4) NOT NULL,
	  `server` smallint(6) NOT NULL,
	  `platform` tinyint(4) NOT NULL DEFAULT '0' COMMENT '추후 변경 가능(삭제 포함)',
	  `market` tinyint(4) NOT NULL DEFAULT '0' COMMENT '추후 변경 가능(삭제 포함)',
	  `user_seq` int(10) NOT NULL,
	  `dungeon_reward_card_type` tinyint(4) NOT NULL,
	  `dungeon_reward_item_type` tinyint(4) NOT NULL,
	  `dungeon_reward_item_name` tinyint(4) NOT NULL,
	  `dungeon_reward_item_grade` varchar(3) NOT NULL,
	  `dungeon_reward_item_refine_grade` tinyint(4) NOT NULL,
	  `provide_success` enum('0','1') NOT NULL,
	  PRIMARY KEY (`seq`)
	) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8 COMMENT='DUNGEON REWARD LOG'
	 *
	 */

	int dungeon_reward_card_type = 0;
	int dungeon_reward_item_type = 0;
	int dungeon_reward_item_name = 0;
	char dungeon_reward_item_grade = 's';
	int dungeon_reward_item_refine_grade = 0;
	int provide_success = 1;

	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();
	pCommand->SetQueryType(::Database::Command::INSERT);
	pCommand->MakeQuery("INSERT INTO zn_dungeon_reward_log VALUES ("
			"NULL, "
			"NOW(), "
			"0, "
			"0, "
			"0, "
			"%d, "
			"%d, "
			"%d, "
			"%d, "
			"%d, "
			"%d, "
			"'%c', "
			"%d, "
			"%d )",
			pUser->GetCompany(),
			pUser->GetSaleCode(),
			pUser->GetUserSeq(),
			dungeon_reward_card_type,
			dungeon_reward_item_type,
			dungeon_reward_item_name,
			dungeon_reward_item_grade,
			dungeon_reward_item_refine_grade,
			provide_success);
	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LaunchingEvent(User * pUser, int battleType)
{
	/*
	 * 론칭 이벤트 로그 기록
	 * battleType
	 * 	1: PVP
	 * 	2: 파티던전
	 * 	3: 배틀로얄
	 */

	if (false == NResource::Static::InstancePtr()->GetEventTime()->IsLaunchingEvent(pUser))
		return;

	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();
	pCommand->SetQueryType(::Database::Command::S_PROCEDURE);
	pCommand->MakeQuery("call sp_insert_data_event(%d, %d)", pUser->GetUserSeq(), battleType);
	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(pUser->GetUserInfo().shard_Item_db_type, pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void Fail_ValidateData(User * pUser, uint32_t u32sp, uint32_t u32attack, uint32_t u32defence, uint32_t u32str, uint32_t u32dex,
		uint32_t u32con, uint32_t u32int, uint32_t u32avoid_rate, uint32_t u32critical_rate, uint32_t u32critical_damage)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();
	pCommand->SetQueryType(::Database::Command::INSERT);
	pCommand->MakeQuery("INSERT INTO log_data_validate_check VALUES (NULL, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, NOW());",
			pUser->GetUserSeq(), pUser->GetCharacterInfo().slotindex, pUser->GetCharacterInfo().level, pUser->GetCharacterInfo().gold,
			u32str, u32dex, u32con, u32int, u32sp, u32attack, u32defence, u32avoid_rate, u32critical_rate, u32critical_damage );

	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LogTradeBuyInfo(User * pUser, uint32_t sellUserSeq, uint32_t itemPrice, uint32_t itemClassType,
		uint32_t itemSheetType, uint64_t tradeSeq, uint64_t itemRegDate, uint32_t itemQuality, const char * itemName)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();

	pCommand->SetQueryType(::Database::Command::S_PROCEDURE);
	pCommand->MakeQuery("call sp_insert_trade_info(1, %lld,%lld,'%s','%s','%s',%d,%d,%lld,'%s',%d,%d,%d,%d,%lld,%lld)"
			,sellUserSeq
			,pUser->GetUserSeq()
			,pUser->GetUserNickName().c_str()
			,pUser->Getuuid().c_str()
			,pUser->GetSocialID().c_str()
			,pUser->GetCompany()
			,pUser->GetSaleCode()
			,pUser->GetCharacterInfo().characterseq
			,itemName
			,itemQuality
			,itemPrice
			,itemClassType
			,itemSheetType
			,tradeSeq
			,itemRegDate);

	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LogTradeRegInfo(User * pUser, uint32_t itemPrice, uint32_t itemClassType,
		uint32_t itemSheetType, uint64_t tradeSeq, uint64_t itemRegDate, uint32_t itemQuality, const char * itemName)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();

	pCommand->SetQueryType(::Database::Command::S_PROCEDURE);
	pCommand->MakeQuery("call sp_insert_trade_info(0, %lld,0,'','','',0,0,0,'%s',%d,%d,%d,%d,%lld,%lld)"
			,pUser->GetUserSeq()
			,itemName
			,itemQuality
			,itemPrice
			,itemClassType
			,itemSheetType
			,tradeSeq
			,itemRegDate);

	DEBUGLOG("Query=[%s]", pCommand->GetQueryString());

	pCommand->SetObserver(Static::Instance().GetMySql());
	pCommand->SetCommandId(DBCommand::DB_LOG_ITEM_TRADE_INFO);

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LogGoldInfo(User * pUser, int kind, int changeGold)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();

	pCommand->SetQueryType(::Database::Command::S_PROCEDURE);
	pCommand->MakeQuery("insert into log_user_gold_info (seq ,user_seq ,character_seq ,kind ,change_gold ,now_gold, reg_time) VALUES(NULL, %d, %d, %d, %d, %d, UNIX_TIMESTAMP(now()))",
			pUser->GetUserSeq(), pUser->GetCharacterInfo().characterseq, kind, changeGold, pUser->GetCharacterInfo().gold);

	pCommand->SetObserver(Static::Instance().GetMySql());
	pCommand->SetCommandId(DBCommand::DB_LOG_GOLD_INFO);

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LogItemInfo(User * pUser, int kind, int result, int user_goods, NLogic::Item * pItem)
{
	::Database::Command * pCommand = ::Database::GETCOMMANDPOOL()->NEW();

	pCommand->SetQueryType(::Database::Command::S_PROCEDURE);
	pCommand->MakeQuery("INSERT INTO log_item_info VALUES (NULL,%d,%d,%d, %d,%d,%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%lld,UNIX_TIMESTAMP(now()))",
			kind
			,result
			,user_goods
			,pUser->GetUserSeq()
			,pUser->GetCharacterInfo().characterseq
			,pItem->GetItemSeq()
			,pItem->GetData().bag_type
			,pItem->GetData().bag_slot_number
//			,pItem->GetData().stack_num
			,pItem->GetData().quantity
			,pItem->GetData().class_type
			,pItem->GetData().sub_type
			,pItem->GetData().tid
			,pItem->GetData().iconidx
			,pItem->GetData().item_type
			,pItem->GetData().cur_duration
			,pItem->GetData().max_duration
			,pItem->GetData().set_type
			,pItem->GetData().non_identity
			,pItem->GetData().cur_refine_step
			,pItem->GetData().quality
			,pItem->GetData().level
			,pItem->GetData().eff_type[0]
			,pItem->GetData().eff_pos[0]
			,pItem->GetData().eff_value[0]
			,pItem->GetData().eff_type[1]
			,pItem->GetData().eff_pos[1]
			,pItem->GetData().eff_value[1]
			,pItem->GetData().eff_type[2]
			,pItem->GetData().eff_pos[2]
			,pItem->GetData().eff_value[2]
			,pItem->GetData().eff_type[3]
			,pItem->GetData().eff_pos[3]
			,pItem->GetData().eff_value[3]
			,pItem->GetData().eff_type[4]
			,pItem->GetData().eff_pos[4]
			,pItem->GetData().eff_value[4]
			,pItem->GetData().eff_type[5]
			,pItem->GetData().eff_pos[5]
			,pItem->GetData().eff_value[5]
			,pItem->GetData().eff_type[6]
			,pItem->GetData().eff_pos[6]
			,pItem->GetData().eff_value[6]
			,pItem->GetData().open_upgrade_stone_slot
			,pItem->GetData().ability_enhance_rate
			,pItem->GetData().max_enhance_step
			,pItem->GetData().buy_use
			,pItem->GetData().evolve_step
			,pItem->GetData().evolve_max
			,pItem->GetData().evolve_point
			,pItem->GetData().evolve_percent
			,pItem->GetData().evolve_value
			,pItem->GetData().reg_date
			);

	pCommand->SetObserver(Static::Instance().GetMySql());

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

void LogConnectionInfo(User * pUser, int logoutType)
{
	Database::Command * pCommand = Database::GETCOMMANDPOOL()->NEW();

	time_t logoutTime = time(NULL);
	pCommand->SetQueryType(Database::Command::INSERT);
	pCommand->MakeQuery("insert into log_user_connection_info values (NULL, %d, '%s', %d, %d, %d, %d)",
			pUser->GetUserSeq(), pUser->Getuuid().c_str(), pUser->GetLoginTime(), logoutTime, logoutTime - pUser->GetLoginTime(), logoutType);

	pCommand->SetObserver(Static::Instance().GetMySql());
	pCommand->SetCommandId(DBCommand::DB_LOG_CONNECT_INFO);

	if (false == ::Database::GETSESSION()->AsyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), pCommand))
	{
		Database::GETCOMMANDPOOL()->DEL(pCommand);
		return;
	}
}

} /*NGameLog*/ } /*ProjectZ*/




